package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.domain.SysStock;
import com.ruoyi.system.domain.SysStockDetails;
import com.ruoyi.system.mapper.SysGoodsMapper;
import com.ruoyi.system.mapper.SysStockDetailsMapper;
import com.ruoyi.system.mapper.SysStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysStoreStockMapper;
import com.ruoyi.system.domain.SysStoreStock;
import com.ruoyi.system.service.ISysStoreStockService;

/**
 * 商品库存信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysStoreStockServiceImpl implements ISysStoreStockService {
    @Autowired
    private SysStoreStockMapper sysStoreStockMapper;
    @Autowired
    private SysStockMapper sysStockMapper;
    @Autowired
    private SysGoodsMapper sysGoodsMapper;
    @Autowired
    private SysStockDetailsMapper sysStockDetailsMapper;

    /**
     * 查询商品库存信息
     *
     * @param id 商品库存信息主键
     * @return 商品库存信息
     */
    @Override
    public SysStoreStock selectSysStoreStockById(String id) {
        return sysStoreStockMapper.selectSysStoreStockById(id);
    }

    /**
     * 查询商品库存信息列表
     *
     * @param sysStoreStock 商品库存信息
     * @return 商品库存信息
     */
    @Override
    public List<SysStoreStock> selectSysStoreStockList(SysStoreStock sysStoreStock) {
        return sysStoreStockMapper.selectSysStoreStockList(sysStoreStock);
    }

    /**
     * 新增商品库存信息
     *
     * @param sysStoreStock 商品库存信息
     * @return 结果
     */
    @Override
    public int insertSysStoreStock(SysStoreStock sysStoreStock) {
        SysGoods sysGoods = sysGoodsMapper.selectSysGoodsById(sysStoreStock.getGoodsId());
        sysStoreStock.setCategoryId(sysGoods.getCategoryId());
        sysStoreStock.setCreateTime(DateUtils.getNowDate());

        //库存明细新增数据
        SysStockDetails sysStockDetails = new SysStockDetails();
        sysStockDetails.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        sysStockDetails.setGoodsId(sysStoreStock.getGoodsId());
        sysStockDetails.setStoreId(sysStoreStock.getStoreId());
        // 默认入库类型
        sysStockDetails.setAdjustType("1");
        sysStockDetails.setAdjustNum(sysStoreStock.getStockNum());
        sysStockDetails.setRemainingNum(sysStoreStock.getStockNum());
        sysStockDetails.setCreateTime(DateUtils.getNowDate());
        sysStockDetailsMapper.insertSysStockDetails(sysStockDetails);

        return sysStoreStockMapper.insertSysStoreStock(sysStoreStock);
    }

    /**
     * 修改商品库存信息
     *
     * @param sysStoreStock 商品库存信息
     * @return 结果
     */
    @Override
    public int updateSysStoreStock(SysStoreStock sysStoreStock) {
        // 1 入库，  2出库，  3盘点调亏，  4盘点调增
        // 判断变更类型
        int resNum = 0;
        if ("1".equals(sysStoreStock.getChangeType())) {
            // 入库处理
            // todo 需要处理 (完成)
            // 创建库存详情对象
            SysStockDetails sysStockDetail = new SysStockDetails();
            sysStockDetail.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            sysStockDetail.setGoodsId(sysStoreStock.getGoodsId());
            sysStockDetail.setStoreId(sysStoreStock.getStoreId());
            sysStockDetail.setAdjustType(sysStoreStock.getChangeType());
            // 查询库存详情列表
            SysStockDetails param = new SysStockDetails();
            param.setGoodsId(sysStoreStock.getGoodsId());
            param.setStoreId(sysStoreStock.getStoreId());
            List<SysStockDetails> sysStockDetails = sysStockDetailsMapper.selectSysStockDetailsList(param);
            if (!sysStockDetails.isEmpty()) {
                SysStockDetails sys = sysStockDetails.get(0);
                //计算历史数据
                sysStockDetail.setAdjustNum(sysStoreStock.getStockNum());
                String remainingNum = sys.getRemainingNum() == null ? "0" : sys.getRemainingNum();
                resNum = Integer.parseInt(remainingNum) + Integer.parseInt(sysStoreStock.getStockNum());
                sysStockDetail.setRemainingNum(String.valueOf(resNum));
                sysStoreStock.setStockNum(String.valueOf(resNum));
                // 插入库存详情
                sysStockDetailsMapper.insertSysStockDetails(sysStockDetail);
            } else {
                SysStoreStock res = sysStoreStockMapper.selectSysStoreStockById(sysStoreStock.getId());
                int num = Integer.parseInt(res.getStockNum()) + Integer.parseInt(sysStoreStock.getStockNum());
                sysStoreStock.setStockNum(num +"");
                sysStockDetail.setAdjustNum("0");
                sysStockDetail.setRemainingNum(sysStoreStock.getStockNum());
                // 插入库存详情
                sysStockDetailsMapper.insertSysStockDetails(sysStockDetail);
            }
        } else if ("2".equals(sysStoreStock.getChangeType())) {
            // 出库处理
            // 出库
            SysStoreStock res = sysStoreStockMapper.selectSysStoreStockById(sysStoreStock.getId());
            int stockNum = Integer.parseInt(res.getStockNum());// 库存数量
            //库存数量大于出库数量
            if (stockNum >= Integer.parseInt(sysStoreStock.getStockNum())) {
                SysStockDetails sysStockDetail = new SysStockDetails();
                sysStockDetail.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                sysStockDetail.setGoodsId(sysStoreStock.getGoodsId());
                sysStockDetail.setStoreId(sysStoreStock.getStoreId());
                sysStockDetail.setAdjustType(sysStoreStock.getChangeType());
                resNum = stockNum - Integer.parseInt(sysStoreStock.getStockNum());
                sysStockDetail.setAdjustNum(sysStoreStock.getStockNum());
                sysStockDetail.setRemainingNum(String.valueOf(resNum));
                // 插入库存详情
                sysStockDetailsMapper.insertSysStockDetails(sysStockDetail);
                sysStoreStock.setStockNum(resNum + "");
            } else {
                // 提示库存数量不足
                return -1;
            }
        }
        // 更新库存信息的时间戳
        sysStoreStock.setUpdateTime(DateUtils.getNowDate());
        return sysStoreStockMapper.updateSysStoreStock(sysStoreStock);
    }


    /**
     * 批量删除商品库存信息
     *
     * @param ids 需要删除的商品库存信息主键
     * @return 结果
     */
    @Override
    public int deleteSysStoreStockByIds(String[] ids) {
        return sysStoreStockMapper.deleteSysStoreStockByIds(ids);
    }

    /**
     * 删除商品库存信息信息
     *
     * @param id 商品库存信息主键
     * @return 结果
     */
    @Override
    public int deleteSysStoreStockById(String id) {
        return sysStoreStockMapper.deleteSysStoreStockById(id);
    }
}
